<?php
namespace lib;

class Wf{

	protected static $dic;
	protected static $dicSale;
	protected static $dicAlert;
	protected static $dicInvasion;

    /**
     * 加载字典
     */
	static function loadDict(){
		if(!self::$dic){
			self::$dic = json_decode(file_get_contents(ROOT_PATH . 'dic/WF_Dict.json'), true);
			self::$dicSale = json_decode(file_get_contents(ROOT_PATH . 'dic/WF_Sale.json'), true);
			self::$dicAlert = json_decode(file_get_contents(ROOT_PATH . 'dic/WF_Alert.json'), true);
			self::$dicInvasion = json_decode(file_get_contents(ROOT_PATH . 'dic/WF_Invasion.json'), true);
		}
	}

	static function explain($words, $type = 'dict'){
		self::loadDict();
		switch ($type){
			case 'dict':
				$dict = self::$dic;
				break;
			case 'sale':
			    $result = false;
				$dict = self::$dicSale;
				foreach ($dict as $item){
					if(trim($words) == $item['Zh']){
                        $result = $item;
                        break;
					}
				}
				if(!$result){
					if(preg_match('/^[\x{4e00}-\x{9fa5}]/u', $words)){
						preg_match('/^([\x{4e00}-\x{9fa5}]+)\s?(\w*?)\s?([\x{4e00}-\x{9fa5}]*?)$/u', $words, $search);
					}else{
						$search = explode(' ', $words);
						array_unshift($search, '');
						$search[1] = strtoupper($search[1]);
						if(!isset($search[3])){
							$search[3] = '';
						}
					}
                    if(isset($search[3]) && $search[3] == '头'){
                        $search[3] = '头部神经光元';
                    }
                    if(strtoupper($search[2]) == 'P'){
                        $search[2] = 'PRIME';
                    }
                    unset($search[0]);
                    $words = implode(' ', $search);
                    foreach ($dict as $item){
                        if(trim($words) == $item['Zh']){
                            $result = $item;
                            break;
                        }
                    }
                    if(!$result){
	                    $words .= '一套';
                        foreach ($dict as $item){
                            if(trim($words) == $item['Zh']){
                                $result = $item;
                                break;
                            }
                        }
                    }
                }
				return $result;
				break;
			case 'alert':
				$dict = self::$dicAlert;
				break;
			case 'invasion':
				$dict = self::$dicInvasion;
				break;
		}
		foreach ($dict as $item){
			if(trim($words) == $item['En']){
				return $item['Zh'];
			}
		}
		return $words;
	}

	static function buildAlert($data = []){
		$msg = [];
		$data = $data ? $data : \lib\Cache::get('alert_data');
		if($data){
			$data = array_reverse($data);
			foreach ($data as $key => $item){
				$body = [];
				$rewards = [];
				$mission = $item['mission'];
				$node = preg_replace_callback('/\((\w*?)\)/', function($arg){
					return '(' . self::explain($arg[1]) . ')';
				}, $mission['node']);
				foreach ($mission['reward']['countedItems'] as $reward){
					array_push($rewards, self::explain($reward['type'], 'alert') . '*' . $reward['count']);
				}
				foreach ($mission['reward']['items'] as $reward){
					array_push($rewards, preg_replace_callback('/[\sA-Za-z]*/', function($arg){
						return self::explain(trim($arg[0]), 'alert');
					}, $reward));
				}
				array_push($body, $node . ' / ' . self::explain($mission['type']));
				$eta = str_replace(['h', 'm', 's'], ['小时', '分', '秒'], $item['eta']);
				if($rewards){
					array_push($body, '*奖励物品: ' . implode('，', $rewards));
				}
				array_push($body, '*剩余时间: ' . $eta);
				array_push($body, '*奖励点数: ' . $mission['reward']['credits'] . ' CR');
//				array_push($body, '*派系: ' . $mission['faction']);
				array_push($body, '*等级: ' . "{$mission['minEnemyLevel']}-{$mission['maxEnemyLevel']}");
				array_push($msg, implode("\n", $body));
			}
			return $msg;
		}else{
			return '正在获取数据...';
		}
	}

	static function buildSortie(){
	    $data = Cache::get('sortie_data');
        if($data){
            $msg = "#派系: {$data['faction']}({$data['boss']})\n\n";
            $variants = $data['variants'];
            $body = [];
            foreach ($variants as $key => $item){
                $node = preg_replace_callback('/\((\w*?)\)/', function($arg){
                    return '(' . self::explain($arg[1]) . ')';
                }, $item['node']);
                $modify = preg_replace_callback('/([\w\s]+)($|[\:\s?]([\w\s]+$))/', function($arg) use ($item) {
                	$res = $arg[1] ? self::explain(trim($arg[1])) : $item['modifier'];
                	if($arg[3]){
		                $res .= ': ' . self::explain(trim($arg[3]));
	                }
                    return $res;
                }, $item['modifier']);
                array_push($body, $node . ' / ' . self::explain($item['missionType']));
                array_push($body, '*BOSS: ' . $item['boss']);
                array_push($body, '*增强: ' . $modify);
                if($key < count($variants) - 1){
                    array_push($body, '------------');
                }
            }
            $msg .= implode("\n", $body);
            $msg .= "\n\n输入'指令'获取功能菜单..";
            return $msg;
        }else{
            return '正在获取数据...';
        }
    }

	static function buildInvasions(){

    }


}